<?php

/**
 * This is the model class for table "nomenclador.prestaciones_permitidas".
 *
 * The followings are the available columns in table 'nomenclador.prestaciones_permitidas':
 * @property integer $id
 * @property integer $id_prestacion
 * @property integer $id_patologia
 * @property integer $grp_patologico
 * @property string $descripcion
 * @property string $precio
 * @property string $requiere_trazadora
 * @property integer $cant_dias
 * @property integer $cant_mes
 * @property integer $cant_anios
 *
 * The followings are the available model relations:
 * @property Prestaciones $idPrestacion
 * @property GrupoPatologico $grpPatologico
 */
class PrestacionesPermitidas extends CActiveRecord
{
     public $categoria_prestacion,
            $patologias,
             $tipo_trazadora;
            
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'nomenclador.prestaciones_permitidas';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_prestacion, id_patologia, grp_patologico, requiere_trazadora', 'required'),
			array('id_prestacion, id_patologia, grp_patologico, cant_dias, cant_mes, cant_anios', 'numerical', 'integerOnly'=>true),
			array('descripcion', 'length', 'max'=>250),
			array('precio', 'length', 'max'=>8),
			array('requiere_trazadora', 'length', 'max'=>1),
                    
                        array('descripcion', 'length', 'max'=>250),
                        array('precio','safe'),
                        array('precio','rulesPrecioValido'),
                        array('patologias','rulesEleccionPatologia'),	
                    
                    
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, id_prestacion, id_patologia, grp_patologico, descripcion, precio, requiere_trazadora, cant_dias, cant_mes, cant_anios', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'MiPrestacion' => array(self::BELONGS_TO, 'Prestaciones', 'id_prestacion'),
			'MiGrupoPatologico' => array(self::BELONGS_TO, 'GrupoPatologico', 'grp_patologico'),
                        'MiPatologia' => array(self::BELONGS_TO, 'Patologia', 'id_patologia'),
                        'MiPatologiaPerinatal'=> array(self::BELONGS_TO, 'PatologiaPerinatal', 'id_patologia'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_prestacion' => 'Id Prestacion',
			'id_patologia' => 'Id Patologia',
			'grp_patologico' => 'Grp Patologico',
			'descripcion' => 'Descripcion',
			'precio' => 'Precio',
			'requiere_trazadora' => 'Requiere Trazadora',
			'cant_dias' => 'Cant Dias',
			'cant_mes' => 'Cant Mes',
			'cant_anios' => 'Cant Anios',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_prestacion',$this->id_prestacion);
		$criteria->compare('id_patologia',$this->id_patologia);
		$criteria->compare('grp_patologico',$this->grp_patologico);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('precio',$this->precio,true);
		$criteria->compare('requiere_trazadora',$this->requiere_trazadora,true);
		$criteria->compare('cant_dias',$this->cant_dias);
		$criteria->compare('cant_mes',$this->cant_mes);
		$criteria->compare('cant_anios',$this->cant_anios);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return PrestacionesPermitidas the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        /**********************************************************/
        /**********************************************************/
        /**********************************************************/
        
         public function rulesPrecioValido(){
             
           $reglas = "/^[0-9]{1,10}(\.[0-9]{1,2}){0,1}$/";           
           if (!(preg_match($reglas, $this->precio)))
              $this->addError('precio','Error!!. PRECIO INVALIDO!!!');  
        }
        
         public function rulesEleccionPatologia(){
            if (empty($this->id_patologia))
              $this->addError('patologias','Error!!. SELECCIONE AL MENOS UNA PTOLOGIA!!!'); 
         }
        
        
        public function getMiTrazadora(){
            if ($this->requiere_trazadora > 0){
               $traz = TipoTrazadora::model()->findByPk($this->requiere_trazadora);    
               return "SI:  " . $traz->descripcion;}
            else
                return "NO";
            
        }
}
